//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.app.twth.satoprinter.utility;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class Utility {
    public Utility() {
    }

    public static String toHexString(byte[] bytes) {
        if (bytes != null && bytes.length > 0) {
            StringBuffer buffer = new StringBuffer();

            for (int i = 0; i < bytes.length; ++i) {
                String hexString = Integer.toHexString(bytes[i] & 255);
                if (hexString.length() == 1) {
                    buffer.append('0');
                }

                buffer.append(hexString);
                if (i < bytes.length - 1) {
                    buffer.append(' ');
                }
            }

            return buffer.toString();
        } else {
            return null;
        }
    }

    public static String toHexString(int i) {
        StringBuffer hexString = new StringBuffer(Integer.toHexString(i));
        if (hexString.length() == 1) {
            hexString.insert(0, "0x0");
        } else {
            hexString.insert(0, "0x");
        }

        return hexString.toString();
    }

    public static byte[] getBinary(Context context, int id) {
        Resources resource = context.getResources();
        InputStream in = resource.openRawResource(id);
        BufferedInputStream bis = new BufferedInputStream(in);
        boolean var5 = false;

        int bufferLength;
        try {
            bufferLength = bis.available();
        } catch (IOException var10) {
            var10.printStackTrace();
            return null;
        }

        byte[] buffer = new byte[bufferLength];
        int read = 0;

        try {
            read = bis.read(buffer);
        } catch (IOException var9) {
            var9.printStackTrace();
        }

        return read > 0 ? buffer : null;
    }

    public static byte[] getBinary(Context context, String fileName) {
        AssetManager assetManager = context.getAssets();
        InputStream in = null;

        try {
            in = assetManager.open(fileName);
        } catch (IOException var11) {
            var11.printStackTrace();
            return null;
        }

        BufferedInputStream bis = new BufferedInputStream(in);
        boolean var5 = false;

        int bufferLength;
        try {
            bufferLength = bis.available();
        } catch (IOException var10) {
            var10.printStackTrace();
            return null;
        }

        byte[] buffer = new byte[bufferLength];
        int read = 0;

        try {
            read = bis.read(buffer);
        } catch (IOException var9) {
            var9.printStackTrace();
        }

        return read > 0 ? buffer : null;
    }

    public static byte[] stringToBytes(String hexString) {
        ByteBuffer buffer = ByteBuffer.allocate(hexString.length() / 2);
        String[] var5;
        int var4 = (var5 = hexString.split("\\s+")).length;

        for (int var3 = 0; var3 < var4; ++var3) {
            String token = var5[var3];
            buffer.put(Byte.parseByte(token, 16));
        }

        return copyOfRange(buffer.array(), 0, buffer.position());
    }

    public static byte[] copyOfRange(byte[] original, int start, int end) {
        if (start > end) {
            throw new IllegalArgumentException();
        } else {
            int originalLength = original.length;
            if (start >= 0 && start <= originalLength) {
                int resultLength = end - start;
                int copyLength = Math.min(resultLength, originalLength - start);
                byte[] result = new byte[resultLength];
                System.arraycopy(original, start, result, 0, copyLength);
                return result;
            } else {
                throw new ArrayIndexOutOfBoundsException();
            }
        }
    }
}
